//
//  UIImage+Extension.m
//  School+
//
//  Created by Sean Sebastian on 5/16/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

- (UIImage *)scaleImage
{
    CGFloat newWidth = 300;
    if (self.size.width < newWidth) {
        return self;
    }
    
    // 等比例缩放
    // newHeight / newWidth = 原来的高度 / 原来的宽度
    CGFloat newHeight = newWidth * self.size.height / self.size.width;  // key code !
    CGSize newSize = CGSizeMake(newWidth, newHeight);
    
    // 准备图片的上下文
    UIGraphicsBeginImageContext(newSize);
    
    // 将当前图片绘制到rect上面
    [self drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
    
    // 从上下文获取绘制好的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
